package com.ruoyi.mushcoin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.mushcoin.domain.bo.TgWalletOrderTransactionBo;
import com.ruoyi.mushcoin.domain.vo.TgWalletOrderTransactionImportVo;
import com.ruoyi.mushcoin.domain.vo.TgWalletOrderTransactionVo;
import com.ruoyi.mushcoin.listener.TgWalletOrderTransactionImportListener;
import com.ruoyi.mushcoin.service.ITgWalletOrderTransactionService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * TG钱包订单交易(Ton...)Controller
 *
 * @author billy
 * 2024-11-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mushcoin/walletOrderTransaction")
@Slf4j
public class TgWalletOrderTransactionController extends BaseController {
    @Resource
    private ITgWalletOrderTransactionService tgWalletOrderTransactionService;

    /**
     * 查询TG钱包订单交易(Ton...)列表
     */
    @SaCheckPermission("mushcoin:walletOrderTransaction:list")
    @GetMapping("/list")
    public TableDataInfo<TgWalletOrderTransactionVo> list(TgWalletOrderTransactionBo tgWalletOrderTransactionBo) {
        return tgWalletOrderTransactionService.selectPage(tgWalletOrderTransactionBo);
    }

    /**
     * 导出TG钱包订单交易(Ton...)列表
     */
    @SaCheckPermission("mushcoin:walletOrderTransaction:export")
    @Log(title = "TG钱包订单交易(Ton...)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TgWalletOrderTransactionBo tgWalletOrderTransactionBo) {
        List<TgWalletOrderTransactionVo> list = tgWalletOrderTransactionService.selectList(tgWalletOrderTransactionBo);
        ExcelUtil.exportExcel(list, "TG钱包订单交易(Ton...)", TgWalletOrderTransactionVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "TG钱包订单交易(Ton...)", businessType = BusinessType.IMPORT)
    @SaCheckPermission("mushcoin:walletOrderTransaction:import")
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<TgWalletOrderTransactionImportVo> result = ExcelUtil.importExcel(file.getInputStream(), TgWalletOrderTransactionImportVo.class, new TgWalletOrderTransactionImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @SaCheckPermission("mushcoin:walletOrderTransaction:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "TG钱包订单交易(Ton...)", TgWalletOrderTransactionImportVo.class, response);
    }

    /**
     * 获取TG钱包订单交易(Ton...)详细信息
     */
    @SaCheckPermission("mushcoin:walletOrderTransaction:query")
    @GetMapping(value = "/{id}")
    public R<TgWalletOrderTransactionVo> getInfo(@PathVariable Long id) {
        return R.ok(tgWalletOrderTransactionService.selectById(id));
    }

    /**
     * 新增TG钱包订单交易(Ton...)
     */
    @SaCheckPermission("mushcoin:walletOrderTransaction:add")
    @Log(title = "TG钱包订单交易(Ton...)", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody TgWalletOrderTransactionBo tgWalletOrderTransactionBo) {
        boolean inserted = tgWalletOrderTransactionService.insert(tgWalletOrderTransactionBo);
        if (!inserted) {
            return R.fail("新增TG钱包订单交易(Ton...)记录失败！");
        }
        return R.ok();
    }

    /**
     * 修改TG钱包订单交易(Ton...)
     */
    @SaCheckPermission("mushcoin:walletOrderTransaction:edit")
    @Log(title = "TG钱包订单交易(Ton...)", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody TgWalletOrderTransactionBo tgWalletOrderTransactionBo) {
        Boolean updated = tgWalletOrderTransactionService.update(tgWalletOrderTransactionBo);
        if (!updated) {
            return R.fail("修改TG钱包订单交易(Ton...)记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除TG钱包订单交易(Ton...)
     */
    @SaCheckPermission("mushcoin:walletOrderTransaction:remove")
    @Log(title = "TG钱包订单交易(Ton...)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = tgWalletOrderTransactionService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除TG钱包订单交易(Ton...)记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除TG钱包订单交易(Ton...)
     */
    @SaCheckPermission("mushcoin:walletOrderTransaction:edit")
    @Log(title = "TG钱包订单交易(Ton...)", businessType = BusinessType.DELETE)
    @PostMapping("/payOk/{ids}")
    public R<Void> walletOrderTransactionPayOk(@PathVariable Long[] ids) {
        log.info("walletOrderTransactionPayOk: ids = {}", ids);

        boolean update = false;
        for (Long id : ids) {
            update = tgWalletOrderTransactionService.acceptTonOrderTransaction(id);
        }
        if (!update) {
            return R.fail("修改TG钱包订单交易(Ton...)记录失败!");
        }
        return R.ok();
    }
}
